
<script type="text/javascript">
<!--
var total_amount = $("input[name=total_amount]");
var trade_amount = $("input[name=trade_amount]");
var odd_amount = $("input[name=odd_amount]");

$(function(){
	
	// 初始化應收金額和實收金額
	total_amount.val((0).toFixed(2));
	trade_amount.val((0).toFixed(2));
	
	// 扣除零頭
	odd_amount.keyup(function(){
		trade_amount.val((total_amount.val() - $(this).val()).toFixed(2));
	});
});

function calculate(t, del){
    var tbody = $(t).parents('tbody');
    var total = 0;
    var fix = 0; // 刪除的話應該忽略本行
    if (del == 1){
        $(t).parents("tr:first").each(function(){
            var count = 1;
            var price = 0;
            var discount = 1;
            var subtotal = 0;
            $(this).find('input').each(function(){
                if (this.name == 'items.count[]') count = this.value == '' ? 1 : this.value;
                if (this.name == 'items.price[]') price = this.value == '' ? 0 : this.value;
                if (this.name == 'items.discount[]') discount = this.value == '' ? 1 : (1 - this.value/100);
                if (this.name == 'items.subtotal[]'){
                    subtotal = count*price*discount;
                    if (subtotal == 0){
                        this.value = '';
                    } else {
                        this.value = subtotal.toFixed(2);
                    }
                }
            });
            fix = subtotal;
        });
    }
    $(tbody).find('tr').each(function(){
        var count = 1;
        var price = 0;
        var discount = 1;
        var subtotal = 0;
        $(this).find('input').each(function(){
            if (this.name == 'items.count[]') count = this.value == '' ? 1 : this.value;
            if (this.name == 'items.price[]') price = this.value == '' ? 0 : this.value;
            if (this.name == 'items.discount[]') discount = this.value == '' ? 1 : (1 - this.value/100);
            if (this.name == 'items.subtotal[]'){
                subtotal = count*price*discount;
                if (subtotal == 0){
                    this.value = '';
                } else {
                    this.value = subtotal.toFixed(2);
                }
            }
        });
        total += subtotal;
    });
    if (fix != 0){
        total -= fix;
    }
    total_amount.val(total.toFixed(2));
    trade_amount.val((total - odd_amount.val()).toFixed(2));
}
//-->
</script>


<div class="pageContent">
	
	<form method="post" action="__URL__/insert/navTabId/__MODULE__" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone)">
		<table class="list nowrap itemDetail" addButton="添加貨物" width="100%">
				<thead>
					<tr>
						<th type="lookup" name="items.item_num[]" lookupGroup="items" lookupUrl="__APP__/Itemlect/orderType/0" suggestFields="item_num" fieldClass="readonly" size="10" readOnly="readonly">貨物編碼</th>
						<th type="text" name="items.item_name[]" fieldClass="readonly" size="20" readOnly="readonly">貨物名稱</th>
						<th type="text" name="items.spec[]" fieldClass="readonly" size="10" readOnly="readonly">規 格</th>
						<th type="text" name="items.unit[]" fieldClass="readonly" size="10" readOnly="readonly">單 位</th>
						<th type="calculate" name="items.count[]" fieldClass="required" size="10">數 量</th>
						<th type="calculate" name="items.price[]" fieldClass="required" size="10">單價(元)</th>
						<!--<th type="calculate" name="items.discount[]" size="12">折扣(%)</th>-->
						<th type="text" name="items.subtotal[]" fieldClass="readonly" size="12" readOnly="readonly">小計(元)</th>
						<th type="del" fieldClass="refreshAmount">操 作</th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
	</form>	
</div>

<script>
function tdHtml(field){
                var html='',suffix='';
                if(field.name.endsWith("[#index#]"))suffix="[#index#]";
                else if(field.name.endsWith("[]"))suffix="[]";
                var suffixFrag=suffix?' suffix="'+suffix+'" ':'';
                switch(field.type){
                    case 'del':
                        html='<a href="javascript:void(0)" class="btnDel '+field.fieldClass+'">删除</a>';
                        break;
                    case 'lookup':
                        var suggestFrag='';
                        if(field.suggestFields){
                            suggestFrag='autocomplete="off" lookupGroup="'+field.lookupGroup+'"'+suffixFrag+' suggestUrl="'+field.suggestUrl+'" suggestFields="'+field.suggestFields+'"'+' postField="'+field.postField+'"';}
                        html='<input type="hidden" name="'+field.lookupGroup+'.'+field.lookupPk+suffix+'"/>'+'<input type="text" name="'+field.name+'"'+suggestFrag+' lookupPk="'+field.lookupPk+'" size="'+field.size+'" class="'+field.fieldClass+'"'+field.readOnly+'/>'+'<a class="btnLook" href="'+field.lookupUrl+'" lookupGroup="'+field.lookupGroup+'" '+suggestFrag+' lookupPk="'+field.lookupPk+'" title="查找带回">查找带回</a>';
                        break;
                    case 'attach':
                        html='<input type="hidden" name="'+field.lookupGroup+'.'+field.lookupPk+suffix+'"/>'+'<input type="text" name="'+field.name+'" size="'+field.size+'" class="'+field.fieldClass+'"'+field.readOnly+'/>'+'<a class="btnAttach" href="'+field.lookupUrl+'" lookupGroup="'+field.lookupGroup+'" '+suggestFrag+' lookupPk="'+field.lookupPk+'" width="560" height="300" title="查找带回">查找带回</a>';
                        break;
                    case 'enum':
                        $.ajax({
                            type:"POST",dataType:"html",async:false,
                            url:field.enumUrl,
                            data:{inputName:field.name},
                            success:function(response){
                                html=response;}});
                        break;
                    case 'date':
                        html='<input type="text" name="'+field.name+'" value="'+field.defaultVal+'" class="date '+field.fieldClass+'" format="'+field.patternDate+'" size="'+field.size+'"'+field.readOnly+'/>'+'<a class="inputDateButton" href="javascript:void(0)">选择</a>';
                        break;
                    case 'calculate':
                        html = '<input type="text" name="' + field.name + '" value="' + field.defaultVal + '" size="' + field.size + '" onkeyup="calculate(this, 0);" class="' + field.fieldClass + '"'+field.readOnly+'/>';
                        break;
                    default:
                        html='<input type="text" name="'+field.name+'" value="'+field.defaultVal+'" size="'+field.size+'" class="'+field.fieldClass+'" '+field.readOnly+'/>';
                        break;}
                return '<td>'+html+'</td>';}
                
                
                
</script>


